 package com.szholly.pro.watergas_server.ps.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.common.web.MessageException;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.RowBase;

import net.sf.json.JSONObject;

/**
 * 数据有效性统计
 * @author gislin
 *
 */
/**
 * @author gislin
 *
 */
/**
 * @author gislin
 *
 */
@Controller
@RequestMapping("/DataValidateLin/*")
public class DataValidateController extends BaseController{ 
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	/**
	 * 获取数据有效性
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryMonitorDataValidationStatisticsTotal")
	public void queryMonitorDataValidationStatisticsTotal(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		
		List<RowBase> allResult = new ArrayList<RowBase>();
		List<RowBase> sampleComparisonResult = getSampleComparison();
		List<RowBase> standardVerification = getStandardVerification();
		List<RowBase> standardSubstance = getStandardSubstance();
		List<RowBase> exceedComparisonResult = getExceedComparison();
		List<RowBase> inspectionResult = getInspectionRecord();
		List<RowBase> maintenanceRecord = getMaintenanceRecord();
		
		allResult.addAll(sampleComparisonResult);
		allResult.addAll(standardVerification);
		allResult.addAll(standardSubstance);
		allResult.addAll(exceedComparisonResult);
		allResult.addAll(inspectionResult);
		allResult.addAll(maintenanceRecord);
		JSONObject jsonResult = DataUtil.listToJSONObject(allResult, "POLLDATA");
		
		outString(jsonResult.toString());
	}
	
	/**
	 * 获取数据有效性
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryExceedComparisonValidation")
	public void queryExceedComparisonValidation(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		
		List<RowBase> allResult = new ArrayList<RowBase>();
		List<RowBase> exceedComparisonResult = getExceedComparison();
		
		allResult.addAll(exceedComparisonResult);
		JSONObject jsonResult = DataUtil.listToJSONObject(allResult, "POLLDATA");
		outString(jsonResult.toString());
	}
	
	/**
	 * 获取水样比对有效性
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/querySampleComparisonValidation")
	public void querySampleComparisonValidation(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		
		List<RowBase> allResult = new ArrayList<RowBase>();
		List<RowBase> exceedComparisonResult = getSampleComparison();
		
		allResult.addAll(exceedComparisonResult);
		JSONObject jsonResult = DataUtil.listToJSONObject(allResult, "POLLDATA");
		outString(jsonResult.toString());
	}
	
	/**
	 * 获取标样核查有效性
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryStandardVerificationValidation")
	public void queryStandardVerificationValidation(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		
		List<RowBase> allResult = new ArrayList<RowBase>();
		List<RowBase> exceedComparisonResult = getStandardVerification();
		
		allResult.addAll(exceedComparisonResult);
		JSONObject jsonResult = DataUtil.listToJSONObject(allResult, "POLLDATA");
		outString(jsonResult.toString());
	}
	
	/**
	 * 获取标准物质更换有效性
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryStandardSubstanceValidation")
	public void queryStandardSubstanceValidation(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		
		List<RowBase> allResult = new ArrayList<RowBase>();
		List<RowBase> exceedComparisonResult = getStandardSubstance();
		
		allResult.addAll(exceedComparisonResult);
		JSONObject jsonResult = DataUtil.listToJSONObject(allResult, "POLLDATA");
		outString(jsonResult.toString());
	}
	
	/**
	 * 获取日常巡检有效性
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryInspectionRecordValidation")
	public void queryInspectionRecordValidation(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		
		List<RowBase> allResult = new ArrayList<RowBase>();
		List<RowBase> exceedComparisonResult = getInspectionRecord();
		
		allResult.addAll(exceedComparisonResult);
		JSONObject jsonResult = DataUtil.listToJSONObject(allResult, "POLLDATA");
		outString(jsonResult.toString());
	}
	
	/**
	 * 获取定期维护有效性
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryMaintenanceRecordValidation")
	public void queryMaintenanceRecordValidation(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		
		List<RowBase> allResult = new ArrayList<RowBase>();
		List<RowBase> exceedComparisonResult = getMaintenanceRecord();
		
		allResult.addAll(exceedComparisonResult);
		JSONObject jsonResult = DataUtil.listToJSONObject(allResult, "POLLDATA");
		outString(jsonResult.toString());
	}
	
	
	/**
	 * 超标比对有效性
	 * gislin
	 * 2015年11月20日
	 * @return
	 * @throws MessageException 
	 */
	public List<RowBase> getExceedComparison() throws MessageException{
		String wwtpId = RequestUnit.getParameter("WWTP_ID"); //污水厂id
		String flowType = RequestUnit.getString("FLOW_NAME"); //流向
		String year = RequestUnit.getString("MEASURE_YEAR"); //年份
		String startMonth = RequestUnit.getString("MEASURE_START_MONTH"); //开始时间
		String endMonth = RequestUnit.getString("MEASURE_END_MONTH"); //结束时间
		String stationId ;
		Date startDate = null;
		Date endDate = null;
		
		try {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			startDate = sf.parse(year+"-"+startMonth+"-01");
			endDate = sf.parse(year+"-"+endMonth+"-01");
		} catch (ParseException e) {
			throw new MessageException("无效时间");
		}
		
		stationId = getStationId(wwtpId, flowType);
		
		String allCountSql = " SELECT count(*) from OP_TASK ot \n "
				+" WHERE ot.JOB_ID = '2bf6c8b1-12c3-496c-8e1b-96b52157e12c' " 
				+"         AND ot.START_TIME >= @startDate \n" 
				+"         AND ot.START_TIME <= @endDate \n" 
				+"         AND ot.STATION_ID = @stationId \n";
		
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("stationId", stationId));
		parms.add(new GlobalParameter("startDate", startDate));
		parms.add(new GlobalParameter("endDate", endDate));
		
		int taskCount = DataUtil.getScaleInt(allCountSql, parms);
		
		//统计每个参数完成的任务数
		String checkTaskTimeIntervalSql = 
				"select '超标比对' AS JOB_NAME, '24小时内' AS JOB_CYCLE_DESC,"+taskCount+" MEASURE_STD_NUM,taskCount.MEASURE_TIMELY_NUM/"+taskCount+" taskCountPre,"
						+ "taskCount.*, taskValidate.taskValidateCount MEASURE_ACCURACY_NUM, taskValidate.taskValidateCount/"+taskCount+" taskValidateCountPre from (\n" +
				"  select count(*) MEASURE_TIMELY_NUM,t.measure_item PARMNAME from (\n" + 
				"    SELECT oc.measure_item,oc.task_id,COUNT(*) taskCount\n" + 
				"       from OP_TASK ot\n" + 
				"       inner join OP_EXCEED_COMP oc ON oc.task_id = ot.task_code\n" + 
				"       WHERE ot.JOB_ID = '2bf6c8b1-12c3-496c-8e1b-96b52157e12c'\n" + 
				"         AND ot.START_TIME >= @startDate\n" + 
				"         AND ot.START_TIME <= @endDate\n" + 
				"         AND ot.TASK_STATUS = '1'\n" + 
				"         AND ot.STATION_ID = @stationId \n" + 
				"         group by oc.measure_item,oc.task_id\n" + 
				"  ) t group by t.measure_item\n" + 
				") taskCount \n" + 
				"left join\n" + 
				"(\n" + 
				"  select count(*) taskValidateCount,t.measure_item parm from (\n" + 
				"    SELECT oc.measure_item,oc.task_id,COUNT(*) taskCount,sum(oc.MEASURE_CONCLUSION) CONCLUSION\n" + 
				"       from OP_TASK ot\n" + 
				"       inner join OP_EXCEED_COMP oc ON oc.task_id = ot.task_code\n" + 
				"       WHERE ot.JOB_ID = '2bf6c8b1-12c3-496c-8e1b-96b52157e12c'\n" + 
				"         AND ot.START_TIME >= @startDate\n" + 
				"         AND ot.START_TIME <= @endDate\n" + 
				"         AND ot.TASK_STATUS = '1'\n" + 
				"         AND ot.STATION_ID = @stationId \n" + 
				"         group by oc.measure_item,oc.task_id\n" + 
				"  ) t where t.CONCLUSION >=2 group by t.measure_item\n" + 
				") taskValidate on taskCount.PARMNAME = taskValidate.parm";

		List<RowBase> parmTaskValidateList = DataUtil.getSelectRows(checkTaskTimeIntervalSql, parms);
		for(RowBase row : parmTaskValidateList){
			double parmTaskValidateCount = row.getSafeDouble("taskValidateCountPre"); 
			double parmTaskCount = row.getSafeDouble("taskCountPre"); 
			
			if(parmTaskValidateCount >=0.75 && parmTaskCount >= 0.75){
				row.setValue("JOB_VALIDATION", "有效");
			}
			else{
				row.setValue("JOB_VALIDATION", "无有效");
			}
		}
		return parmTaskValidateList;
	}
	
	/**
	 * 水样比对有效性
	 * gislin
	 * 2015年11月20日
	 * @throws Exception 
	 */
	public List<RowBase> getSampleComparison() throws Exception{
		String wwtpId = RequestUnit.getParameter("WWTP_ID"); //污水厂id
		String flowType = RequestUnit.getString("FLOW_NAME"); //流向
		String year = RequestUnit.getString("MEASURE_YEAR"); //年份
		String startMonth = RequestUnit.getString("MEASURE_START_MONTH"); //开始时间
		String endMonth = RequestUnit.getString("MEASURE_END_MONTH"); //结束时间
		String stationId ;
		Date startDate = null;
		Date endDate = null;
		int monthDiff = 0; //月份差，水样比对是每个月有一次任务，月份差也就是任务的次数
		
		try {
			monthDiff = Integer.parseInt(endMonth) - Integer.parseInt(startMonth)+1;
		} catch (Exception e) {
			throw new MessageException("无效月份");
		}
		
		if(monthDiff < 1){
			throw new MessageException("无效月份");
		}
		
		try {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			startDate = sf.parse(year+"-"+startMonth+"-01");
			endDate = sf.parse(year+"-"+endMonth+"-01");
		} catch (ParseException e) {
			throw new MessageException("无效时间");
		}
		
		stationId = getStationId(wwtpId, flowType);
		
		//统计每个参数完成的任务数
		String checkTaskTimeIntervalSql = 
				"select '水样比对' AS JOB_NAME, '1个月' AS JOB_CYCLE_DESC,"+monthDiff+" MEASURE_STD_NUM,taskCount.MEASURE_TIMELY_NUM/"+monthDiff+" taskCountPre,"
						+ "taskCount.*,taskValidate.taskValidateCount MEASURE_ACCURACY_NUM, taskValidate.taskValidateCount/"+monthDiff+" taskValidateCountPre from (\n" +
				"  select count(*) MEASURE_TIMELY_NUM,t.measure_item PARMNAME from (\n" + 
				"    SELECT oc.measure_item,oc.task_id,COUNT(*) taskCount\n" + 
				"       from OP_TASK ot\n" + 
				"       inner join OP_SAMPLE_COMP oc ON oc.task_id = ot.task_code\n" + 
				"       WHERE ot.JOB_ID = '0f9faa25-89e4-4ad6-869f-aa1beb70f7bc'\n" + 
				"         AND ot.OPERATIONS_TASK_SCHEDULER_ID IS NOT NULL\n" + 
				"         AND ot.START_TIME >= @startDate\n" + 
				"         AND ot.START_TIME <= @endDate\n" + 
				"         AND ot.TASK_STATUS = '1'\n" + 
				"         AND ot.STATION_ID = @stationId \n" + 
				"         group by oc.measure_item,oc.task_id\n" + 
				"  ) t group by t.measure_item\n" + 
				") taskCount \n" + 
				"left join\n" + 
				"(\n" + 
				"  select count(*) taskValidateCount,t.measure_item parm from (\n" + 
				"    SELECT oc.measure_item,oc.task_id,COUNT(*) taskCount,sum(oc.MEASURE_CONCLUSION) CONCLUSION\n" + 
				"       from OP_TASK ot\n" + 
				"       inner join OP_SAMPLE_COMP oc ON oc.task_id = ot.task_code\n" + 
				"       WHERE ot.JOB_ID = '0f9faa25-89e4-4ad6-869f-aa1beb70f7bc'\n" + 
				"         AND ot.OPERATIONS_TASK_SCHEDULER_ID IS NOT NULL\n" + 
				"         AND ot.START_TIME >= @startDate\n" + 
				"         AND ot.START_TIME <= @endDate\n" + 
				"         AND ot.TASK_STATUS = '1'\n" + 
				"         AND ot.STATION_ID = @stationId \n" + 
				"         group by oc.measure_item,oc.task_id\n" + 
				"  ) t where t.CONCLUSION >=2 group by t.measure_item\n" + 
				") taskValidate on taskCount.PARMNAME = taskValidate.parm";

		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("stationId", stationId));
		parms.add(new GlobalParameter("startDate", startDate));
		parms.add(new GlobalParameter("endDate", endDate));
				
		List<RowBase> parmTaskValidateList = DataUtil.getSelectRows(checkTaskTimeIntervalSql, parms);
		for(RowBase row : parmTaskValidateList){
			double parmTaskValidateCount = row.getSafeDouble("taskValidateCountPre"); 
			double parmTaskCount = row.getSafeDouble("taskCountPre"); 
			
			if(parmTaskValidateCount >=0.75 && parmTaskCount >= 0.75){
				row.setValue("JOB_VALIDATION", "有效");
			}
			else{
				row.setValue("JOB_VALIDATION", "无效");
			}
		}
		return parmTaskValidateList;
	}
	
	/**
	 * 标样核查有效性
	 * gislin
	 * 2015年11月23日
	 * @throws Exception 
	 */
	public List<RowBase> getStandardVerification() throws Exception{
		String wwtpId = RequestUnit.getParameter("WWTP_ID"); 				//污水厂id
		String flowType = RequestUnit.getString("FLOW_NAME"); 				//流向
		String year = RequestUnit.getString("MEASURE_YEAR"); 				//年份
		String startMonth = RequestUnit.getString("MEASURE_START_MONTH"); 	//开始时间
		String endMonth = RequestUnit.getString("MEASURE_END_MONTH"); 		//结束时间
		String stationId ;
		Date startDate = null;
		Date endDate = null;
		int monthDiff = 0; 
		
		try {
			monthDiff = Integer.parseInt(endMonth) - Integer.parseInt(startMonth)+1;
		} catch (Exception e) {
			throw new MessageException("无效月份");
		}
		
		if(monthDiff < 1){
			throw new MessageException("无效月份");
		}
		
		try {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			startDate = sf.parse(year+"-"+startMonth+"-01");
			endDate = sf.parse(year+"-"+endMonth+"-01");
		} catch (ParseException e) {
			throw new MessageException("无效时间");
		}
		
		stationId = getStationId(wwtpId, flowType);
		
		//统计每个参数完成的任务数
		String checkTaskTimeIntervalSql = 
				"select '标样核查' AS JOB_NAME, '1个月' AS JOB_CYCLE_DESC, "+monthDiff+" MEASURE_STD_NUM,taskCount.MEASURE_TIMELY_NUM/"+monthDiff+" taskCountPre,"
						+ "taskCount.*, taskValidate.taskValidateCount MEASURE_ACCURACY_NUM, taskValidate.taskValidateCount/"+monthDiff+" taskValidateCountPre from (\n" +
				"  select count(*) MEASURE_TIMELY_NUM,t.test_project PARMNAME from (\n" + 
				"    SELECT oc.test_project,oc.task_code,COUNT(*) taskCount\n" + 
				"       from OP_TASK ot\n" + 
				"       inner join OP_STAND_VERI oc ON oc.task_code = ot.task_code\n" + 
				"       WHERE ot.JOB_ID = '21c74765-685e-4a89-a625-0fa8f3668fc5'\n" + 
				"         AND ot.OPERATIONS_TASK_SCHEDULER_ID IS NOT NULL\n" + 
				"         AND ot.START_TIME >= @startDate\n" + 
				"         AND ot.START_TIME <= @endDate\n" + 
				"         AND ot.TASK_STATUS = '1'\n" + 
				"         AND ot.STATION_ID = @stationId \n" + 
				"         group by oc.test_project, oc.task_code \n" + 
				"  ) t group by t.test_project\n" + 
				") taskCount \n" + 
				"left join\n" + 
				"(\n" + 
				"  select count(*) taskValidateCount,t.test_project parm from (\n" + 
				"    SELECT oc.test_project,oc.task_code,COUNT(*) taskCount,sum(oc.CONCLUSION) CONCLUSION\n" + 
				"       from OP_TASK ot\n" + 
				"       inner join OP_STAND_VERI oc ON oc.task_code = ot.task_code\n" + 
				"       WHERE ot.JOB_ID = '21c74765-685e-4a89-a625-0fa8f3668fc5'\n" + 
				"         AND ot.OPERATIONS_TASK_SCHEDULER_ID IS NOT NULL\n" + 
				"         AND ot.START_TIME >= @startDate\n" + 
				"         AND ot.START_TIME <= @endDate\n" + 
				"         AND ot.TASK_STATUS = '1'\n" + 
				"         AND ot.STATION_ID = @stationId \n" + 
				"         group by oc.test_project,oc.task_code\n" + 
				"  ) t where t.CONCLUSION >=2 group by t.test_project\n" + 
				") taskValidate on taskCount.PARMNAME = taskValidate.parm";

		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("stationId", stationId));
		parms.add(new GlobalParameter("startDate", startDate));
		parms.add(new GlobalParameter("endDate", endDate));
				
		List<RowBase> parmTaskValidateList = DataUtil.getSelectRows(checkTaskTimeIntervalSql, parms);
		for(RowBase row : parmTaskValidateList){
			double parmTaskValidateCount = row.getSafeDouble("taskValidateCountPre"); 
			double parmTaskCount = row.getSafeDouble("taskCountPre"); 
			
			if(parmTaskValidateCount >=0.75 && parmTaskCount >= 0.75){
				row.setValue("JOB_VALIDATION", "有效");
			}
			else{
				row.setValue("JOB_VALIDATION", "无效");
			}
		}
		return parmTaskValidateList;
	}
	
	
	/**
	 * 标准物质更换有效性
	 * gislin
	 * 2015年11月20日
	 * @throws Exception 
	 */
	public List<RowBase> getStandardSubstance() throws Exception{
		String wwtpId = RequestUnit.getParameter("WWTP_ID"); //污水厂id
		String flowType = RequestUnit.getString("FLOW_NAME"); //流向
		String year = RequestUnit.getString("MEASURE_YEAR"); //年份
		String startMonth = RequestUnit.getString("MEASURE_START_MONTH"); //开始时间
		String endMonth = RequestUnit.getString("MEASURE_END_MONTH"); //结束时间
		String stationId ;
		Date startDate = null;
		Date endDate = null;
		int monthDiff = 0; //月份差，水样比对是每个月有一次任务，月份差也就是任务的次数
		
		try {
			monthDiff = Integer.parseInt(endMonth) - Integer.parseInt(startMonth)+1;
		} catch (Exception e) {
			throw new MessageException("无效月份");
		}
		
		if(monthDiff < 1){
			throw new MessageException("无效月份");
		}
		
		try {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			startDate = sf.parse(year+"-"+startMonth+"-01");
			endDate = sf.parse(year+"-"+endMonth+"-01");
		} catch (ParseException e) {
			throw new MessageException("无效时间");
		}
		
		stationId = getStationId(wwtpId, flowType);
		
		//统计每个参数完成的任务数
		String checkTaskTimeIntervalSql = 
				"select '标准物质更换' AS JOB_NAME, '1个月' AS JOB_CYCLE_DESC,"+monthDiff+" MEASURE_STD_NUM,taskCount.MEASURE_TIMELY_NUM/"+monthDiff+" taskCountPre,"
						+ "taskCount.*,taskValidate.taskValidateCount MEASURE_ACCURACY_NUM, taskValidate.taskValidateCount/"+monthDiff+" taskValidateCountPre from (\n" +
				"  select count(*) MEASURE_TIMELY_NUM,t.param PARMNAME from (\n" + 
				"    SELECT oc.param,oc.task_code,COUNT(*) taskCount\n" + 
				"       from OP_TASK ot\n" + 
				"       inner join OPERATION_STAND_SUBSTANCE oc ON oc.task_code = ot.task_code\n" + 
				"       WHERE ot.JOB_ID = '5385ab20-26ee-4fd0-a419-1cd9174a9921'\n" + 
				"         AND ot.OPERATIONS_TASK_SCHEDULER_ID IS NOT NULL\n" + 
				"         AND ot.START_TIME >= @startDate\n" + 
				"         AND ot.START_TIME <= @endDate\n" + 
				"         AND ot.TASK_STATUS = '1'\n" + 
				"         AND ot.STATION_ID = @stationId \n" + 
				"         group by oc.param,oc.task_code\n" + 
				"  ) t group by t.param\n" + 
				") taskCount \n" + 
				"left join\n" + 
				"(\n" + 
				"  select count(*) taskValidateCount,t.param parm from (\n" + 
				"    SELECT oc.param,oc.task_code,COUNT(*) taskCount,COUNT(*) CONCLUSION\n" + 
				"       from OP_TASK ot\n" + 
				"       inner join OPERATION_STAND_SUBSTANCE oc ON oc.task_code = ot.task_code\n" + 
				"       WHERE ot.JOB_ID = '5385ab20-26ee-4fd0-a419-1cd9174a9921'\n" + 
				"         AND ot.OPERATIONS_TASK_SCHEDULER_ID IS NOT NULL\n" + 
				"         AND ot.START_TIME >= @startDate\n" + 
				"         AND ot.START_TIME <= @endDate\n" + 
				"         AND ot.TASK_STATUS = '1'\n" + 
				"         AND ot.STATION_ID = @stationId \n" + 
				"         group by oc.param,oc.task_code\n" + 
				"  ) t where t.CONCLUSION >=1 group by t.param\n" + 
				") taskValidate on taskCount.PARMNAME = taskValidate.parm";

		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("stationId", stationId));
		parms.add(new GlobalParameter("startDate", startDate));
		parms.add(new GlobalParameter("endDate", endDate));
				
		List<RowBase> parmTaskValidateList = DataUtil.getSelectRows(checkTaskTimeIntervalSql, parms);
		for(RowBase row : parmTaskValidateList){
			double parmTaskValidateCount = row.getSafeDouble("taskValidateCountPre"); 
			double parmTaskCount = row.getSafeDouble("taskCountPre"); 
			
			if(parmTaskValidateCount >=0.75 && parmTaskCount >= 0.75){
				row.setValue("JOB_VALIDATION", "有效");
			}
			else{
				row.setValue("JOB_VALIDATION", "无效");
			}
		}
		return parmTaskValidateList;
	}

	/**
	 * 获取日常巡检有效性记录
	 * gislin
	 * 2015年11月20日
	 * @return
	 * @throws MessageException 
	 */
	public List<RowBase> getInspectionRecord() throws MessageException{
		String wwtpId = RequestUnit.getParameter("WWTP_ID"); //污水厂id
		String flowType = RequestUnit.getString("FLOW_NAME"); //流向
		String year = RequestUnit.getString("MEASURE_YEAR"); //年份
		String startMonth = RequestUnit.getString("MEASURE_START_MONTH"); //开始时间
		String endMonth = RequestUnit.getString("MEASURE_END_MONTH"); //结束时间
		String stationId ;
		Date startDate = null;
		Date endDate = null;
		int weedDiff = 0; //周差，也就是应完成的任务数
		double taskCountPre = 0;//任务准时完成率
		double taskValidatePre = 0; //任务合格率
		
		try {
			weedDiff = Integer.parseInt(endMonth) - Integer.parseInt(startMonth)+1;
		} catch (Exception e) {
			throw new MessageException("无效月份");
		}
		
		if(weedDiff < 1){
			throw new MessageException("无效月份");
		}
		
		try {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			startDate = sf.parse(year+"-"+startMonth+"-01");
			endDate = sf.parse(year+"-"+endMonth+"-01");
			
		} catch (ParseException e) {
			throw new MessageException("无效时间");
		}
		
		if(startDate.after(endDate)){
			throw new MessageException("无效时间");
		}
		
		weedDiff = (int) ((endDate.getTime() - startDate.getTime()) / 1000 / 60 / 60 /24 / 7) ;
		
		stationId = getStationId(wwtpId, flowType);
		
		//获取时间范围内所有的日常巡检记录,统计每个任务的合格率
		String validateSql = " SELECT oc.* from OP_TASK ot "
				+ " inner join OP_INSP_RECORD oc ON oc.task_code = ot.task_code " +
				"         AND ot.OPERATIONS_TASK_SCHEDULER_ID IS NOT NULL\n" + 
				"         AND ot.START_TIME >= @startDate\n" + 
				"         AND ot.START_TIME <= @endDate\n" + 
				"         AND ot.STATION_ID = @stationId \n" +
				"   	 AND ot.TASK_STATUS = '1'" ;
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("stationId", stationId));
		parms.add(new GlobalParameter("startDate", startDate));
		parms.add(new GlobalParameter("endDate", endDate));
		List<RowBase> taskRecords = DataUtil.getSelectRows(validateSql, parms);
		
		String[] columns = taskRecords.get(0).getColumns();
		int goodRecordCount = 0; //合格的日常巡检记录数
		int taskCount = taskRecords.size();//完成的任务数
		taskCountPre = (double)taskCount / weedDiff; //计算按时完成任务数的百分比
		taskCountPre = taskCountPre>1 ? 1 : taskCountPre;
		
		for(RowBase row : taskRecords){
			int score = 0;
			for(String column:columns){
				if(column.equals("GUID") || column.equals("TASK_CODE") || column.equals("CREATE_TIME")){
					continue;
				}
				int currentValue = row.getSafeInt(column);
				if (currentValue == 0){
					score += 1;
				}				
			}
			if((double)score/columns.length >= 0.75){
				goodRecordCount ++;
			}
		}
		
		List<RowBase> rows = new ArrayList<>();
		RowBase resultRow = new RowBase();
		resultRow.setValue("JOB_NAME", "日常巡检");
		resultRow.setValue("JOB_CYCLE_DESC", "1周");
		resultRow.setValue("MEASURE_STD_NUM", weedDiff);
		resultRow.setValue("taskCountPre", taskCountPre);
		resultRow.setValue("MEASURE_TIMELY_NUM", taskCount);
		resultRow.setValue("MEASURE_ACCURACY_NUM", goodRecordCount);
		taskValidatePre = (double)goodRecordCount / taskRecords.size();
		taskValidatePre = taskValidatePre > 1 ? 1 : taskValidatePre;
		resultRow.setValue("TASKVALIDATECOUNTPRE", taskValidatePre);
			
		if(taskValidatePre >= 0.75 && taskCountPre >= 0.75){
			resultRow.setValue("JOB_VALIDATION", "有效");
		}
		else{
			resultRow.setValue("JOB_VALIDATION", "无效");
		}
		
		rows.add(resultRow);
		
		return rows;
	}
	
	

	/**
	 * 获取定期维护有效性记录
	 * gislin
	 * 2015年11月20日
	 * @return
	 * @throws MessageException 
	 */
	public List<RowBase> getMaintenanceRecord() throws MessageException{
		String wwtpId = RequestUnit.getParameter("WWTP_ID"); //污水厂id
		String flowType = RequestUnit.getString("FLOW_NAME"); //流向
		String year = RequestUnit.getString("MEASURE_YEAR"); //年份
		String startMonth = RequestUnit.getString("MEASURE_START_MONTH"); //开始时间
		String endMonth = RequestUnit.getString("MEASURE_END_MONTH"); //结束时间
		String stationId ;
		Date startDate = null;
		Date endDate = null;
		int weedDiff = 0; 			//周差，也就是应完成的任务数
		double taskCountPre = 0;	//任务准时完成率
		double taskValidatePre = 0; //任务合格率
		
		try {
			weedDiff = Integer.parseInt(endMonth) - Integer.parseInt(startMonth)+1;
		} catch (Exception e) {
			throw new MessageException("无效月份");
		}
		
		if(weedDiff < 1){
			throw new MessageException("无效月份");
		}
		
		try {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			startDate = sf.parse(year+"-"+startMonth+"-01");
			endDate = sf.parse(year+"-"+endMonth+"-01");
			
		} catch (ParseException e) {
			throw new MessageException("无效时间");
		}
		
		if(startDate.after(endDate)){
			throw new MessageException("无效时间");
		}
		
		weedDiff = (int) ((endDate.getTime() - startDate.getTime()) / 1000 / 60 / 60 /24 / 7) ;
		
		stationId = getStationId(wwtpId, flowType);
		
		//获取时间范围内所有的日常巡检记录,统计每个任务的合格率
		String validateSql = " SELECT oc.* from OP_TASK ot "
				+ " inner join OP_MAIN_RECORD oc ON oc.task_code = ot.task_code " +
				"         AND ot.OPERATIONS_TASK_SCHEDULER_ID IS NOT NULL\n" + 
				"         AND ot.START_TIME >= @startDate\n" + 
				"         AND ot.START_TIME <= @endDate\n" + 
				"         AND ot.STATION_ID = @stationId \n" +
				"   	 AND ot.TASK_STATUS = '1'" ;
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("stationId", stationId));
		parms.add(new GlobalParameter("startDate", startDate));
		parms.add(new GlobalParameter("endDate", endDate));
		List<RowBase> taskRecords = DataUtil.getSelectRows(validateSql, parms);
		
		String[] columns = taskRecords.get(0).getColumns();
		int goodRecordCount = 0; //合格的日常巡检记录数
		int taskCount = taskRecords.size();//完成的任务数
		taskCountPre = (double)taskCount / weedDiff; //计算按时完成任务数的百分比
		taskCountPre = taskCountPre>1 ? 1 : taskCountPre;
		
		for(RowBase row : taskRecords){
			int score = 0;
			for(String column:columns){
				if(column.equals("GUID") || column.equals("TASK_CODE") || column.equals("CREATE_TIME")){
					continue;
				}
				int currentValue = row.getSafeInt(column);
				if (currentValue == 0){
					score += 1;
				}
			}
			if((double)score/columns.length >= 0.75){
				goodRecordCount ++;
			}
		}
		
		List<RowBase> rows = new ArrayList<>();
		RowBase resultRow = new RowBase();
		resultRow.setValue("JOB_NAME", "定期维护");
		resultRow.setValue("JOB_CYCLE_DESC", "2周");
		resultRow.setValue("MEASURE_STD_NUM", weedDiff);
		resultRow.setValue("taskCountPre", taskCountPre);
		resultRow.setValue("MEASURE_TIMELY_NUM", taskCount);
		resultRow.setValue("MEASURE_ACCURACY_NUM", goodRecordCount);
		taskValidatePre = (double)goodRecordCount / taskRecords.size();
		taskValidatePre = taskValidatePre > 1 ? 1 : taskValidatePre;
		resultRow.setValue("TASKVALIDATECOUNTPRE", taskValidatePre);
			
		if(taskValidatePre >= 0.75 && taskCountPre >= 0.75){
			resultRow.setValue("JOB_VALIDATION", "有效");
		}
		else{
			resultRow.setValue("JOB_VALIDATION", "无效");
		}
		
		rows.add(resultRow);
		
		return rows;
	}
	
	/**
	 * 
	 * gislin
	 * 2015年12月16日
	 * @param wwtpid
	 * @param stationType
	 * @return
	 */
	protected String getDataCollectDevMN(String wwtpid,String stationType){
		
		String stationId = getStationId(wwtpid, stationType);
		String getMNInfoSql = " SELECT DEVICE_NUM FROM BC_COLLECTION_INFO WHERE SITE_ID = '"+stationId+"' and rownum=1";
		Object mnObj = DataUtil.getScale(getMNInfoSql, null);
		String mn = mnObj == null ?null:mnObj.toString();
		return mn;
	}
	
	/**
	 * 
	 */
	protected String getStationId(String wwtpid,String stationType){
		String getStationIdSql = " SELECT ms.EXP_NO FROM BC_COLLECTION_INFO dci"
				+ "	left join BC_SITE_INFO ms ON dci.SITE_ID = ms.EXP_NO"
				+ " WHERE ms.WWTP_ID = @WSCLC_ID AND FLOW_TYPE=@FLOW_TYPE AND rownum = 1";

		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		GlobalParameter wwtpIdParm = new GlobalParameter("WSCLC_ID", wwtpid);
		GlobalParameter stationTypeParm = new GlobalParameter("FLOW_TYPE", stationType);
		parms.add(wwtpIdParm);
		parms.add(stationTypeParm);
		Object stationIdObj = DataUtil.getScale(getStationIdSql, parms);
		String stationId = stationIdObj == null ?null:stationIdObj.toString();
		return stationId;
	}
	
}
